/*
 * DenseVec.cpp
 *
 *  Created on: 2014-5-28
 *      Author: cpp
 */
#include"DenseVec.h"
#include<DenseMatrix.h>
#include"SparseVec.h"
#include<Error.h>
#include<map>
using namespace std;
DenseVec::DenseVec(int l):size(l),data(0){
	data=std::make_unique<double[]>(size);
	for(int i=0;i<size;i++){
		data[i]=0.0;
	}
}

DenseVec::~DenseVec(void){
}

Vec* DenseVec::construct()const{
	return new DenseVec(size);
}


Vec* DenseVec::construct(int s)const{
	return new DenseVec(s);
}

double DenseVec::operator*(const Vec& other)const{
	if(len()!=other.len()){throw SYSTEM_NOT_SUPPORT;}
	double result = 0;
	if (typeid(other).name() == "SparseVec") {
		const SparseVec& sOther = dynamic_cast<const SparseVec&>(other);
		map<int, double>* oData = sOther.getData();
		for (map<int, double>::iterator iter = oData->begin();
					iter != oData->end(); iter++) {
			result += iter->second * get(iter->first);
		}
	}
	else{
		for (int i = 0; i < len(); i++) {
			result += get(i) * other.get(i);
		}
	}
	return result;
}
